jQuery.filebrowserclose = function(){
 $.fn.colorbox.close();
 //$('#bg-overlay,#bg-overlay-content').hide();
 return false;
}

jQuery.filebrowser = function(sess,patch,row){
    $.ajax({
     //async:false,
     cache:false,
     url:'filebrowser.php',
     data:'ops=' + sess + '&objdir=' + patch + row,
     error:function(msg){ },
     success:function(data){
      if(data.length > 0){
        $.colorbox({
          width:'100%',
          height:'100%',
          html:data,
          onComplete: function () {
             var $h = $('#cboxLoadedContent').height();
             $('.file-browser-inter').css({'height' : ($h - 100) + 'px'});
             $('#folder, #files').css({'height' : ($h - 110) + 'px'});
             $('#folder .fb-pad, #files .fb-pad').css({'height' : ($h - 120) + 'px'});
          }
        })
        jQuery.filebrowserupdate(sess,patch + row);
      }
     }
    });
}

jQuery.filebrowserupdate = function(sess,patch){
 $('#folder .fb-pad,#files .fb-pad').html('<div class="fb-loader-center"><img src="template/' + jQuery.template + '/images/loading.gif"></div>');
 var splitted = patch.toString().split("&");
 $('#patch').attr({value:splitted[0]});
 $.ajax({
     //async:false,
     cache:false,
     url:'filebrowser.php',
     data:'ops=' + sess + '&objdir=' + patch + '&dn=folder',
     error:function(msg){ $('#folder .fb-pad').html('error'); },
     success:function(data){
      if(data.length > 0){
        $('#folder .fb-pad').html(data);
      }
     }
    });
  $.ajax({
     //async:false,
     cache:false,
     url:'filebrowser.php',
     data:'ops=' + sess + '&objdir=' + patch + '&dn=files',
     error:function(msg){ $('#files .fb-pad').html('error'); },
     success:function(data){
      if(data.length > 0){
        $('#files .fb-pad').html(data);
      }
     }
    });
}

jQuery.filebrowserdel = function(sess,objdir,obj){
 if(confirm(con + '?')){
  $('#files .fb-pad').html('<div class="fb-loader-center"><img src="template/' + jQuery.template + '/images/loading.gif"></div>');
  $.ajax({
     //async:false,
     cache:false,
     url:'filebrowser.php',
     data:'ops=' + sess + '&objdir=' + objdir + '&obj=' + obj + '&dn=realdel',
     error:function(msg){ alert(jQuery.errors); },
     success:function(data){
      if(data.length > 0){
       $patch = $('#patch').attr('value');
        if($patch.length > 0){
         if(data == 1) {
           $.filebrowserfiles('dn=files&ops=' + sess + '&objdir=' + objdir);
         } else {
           alert($.errors);
         }
        }
      }
     }
    });
 }
}

jQuery.filebrowserfiles = function(patch){
 $('#files .fb-pad').html('<div class="fb-loader-center"><img src="template/' + jQuery.template + '/images/loading.gif"></div>');
 $.ajax({
     async:false,
     cache:false,
     url:'filebrowser.php',
     data:patch + '&hash=' + Math.random() + '&objdir=' + $('#patch').val(),
     error:function(msg){ $('#files .fb-pad').html('error'); },
     success:function(data){
      if(data.length > 0){
        $('#files .fb-pad').html(data);
      }
     }
    });
    var i = $('#imaginebox');
    if(i.length > 0){
    var w = $('#files').width() - 15;
    var h = $('#files').height() - 70;
     $(i).css({'height' : h + 'px','width' : w + 'px'});
    }
}

jQuery.filebrowserfolders = function(patch){
 $('#folder .fb-pad').html('<div class="fb-loader-center"><img src="template/' + jQuery.template + '/images/loading.gif"></div>');
 $.ajax({
     //async:false,
     cache:false,
     url:'filebrowser.php',
     data:patch,
     error:function(msg){ $('#folder .fb-pad').html('error'); },
     success:function(data){
      if(data.length > 0){
        $('#folder .fb-pad').html(data);
      }
     }
    });
}

jQuery.filebrowserparent = function(id,link){
 $('#' + id + ' .fb-pad').html('<div class="fb-loader-center"><img src="template/' + jQuery.template + '/images/loading.gif"></div>');
 $patch = $('#patch').attr('value');
 if($patch.length > 0){
 $.ajax({
     //async:false,
     cache:false,
     url:'filebrowser.php',
     data:link + '&objdir=' + $patch,
     error:function(msg){ $('#' + id + ' .fb-pad').html('error'); },
     success:function(data){
      if(data.length > 0){
        $('#' + id + ' .fb-pad').html(data);
      }
     }
 });
 }
}

jQuery.filebrowserinsert = function(name){
 if($('#field').length > 0 && $('#patch').length > 0){
 	var f = $('#field option:selected').val(), p = $('#patch').val();
 	if($('#' + f).length > 0){
     $('#' + f).val('up' + p + name);
     jQuery.filebrowserclose();
    }
 }
}

jQuery.filebrowserimsinsert = function(name){
  if($('#field').length > 0 && $('#patch').length > 0){
  	var f = $('#field option:selected').val(), p = $('#patch').val();
  	if($('#' + f).length > 0 && name.match(/\.(?:jpe?g|gif|png|bmp)/i)){
  	  	$('#' + f).val('up' + p + name);
    }
  }
}

jQuery.filebrowserimsload = function(){
    if ($('#thumb').length > 0) {
  	    var it = $('#thumb').val();
  	    if (it.length > 0 && it.match(/\.(?:jpe?g|gif|png|bmp)/i)) {
  	    	var id = $("#imgid").attr('value');
            if (id) {
               id++;
               var html = '<div id="imginput' + id + '" style="display:none;">';
               html+= '<table class="work" cellpadding="1" cellspacing="1"><tr><td class="work-lite center" valign="top">';
               html+= '<img src="../' + it + '" width="70">';
               html+= '<p class="pad"><br /><input type="text" size="6" maxlength="7" value="" class="imgcount readonly" readonly="readonly">';
               html+= '</td><td class="work-clip" width="100%">';
               html+= '<fieldset class="standart"><legend>' + all_thumb + '</legend>';
               html+= '<input type="text" name="images[' + id + '][image_thumb]" size="15" maxlength="255" value="' + it + '" class="readonly width" readonly="readonly">';
               html+= '</fieldset>';
               if ($('#imagel').length > 0) {
               	   var i = $('#imagel').val();
               	   if (i.length > 0) {
               	       html+= '<fieldset class="standart"><legend>' + all_img + '</legend>';
                       html+= '<input type="text" name="images[' + id + '][image]" size="15" maxlength="255" value="' + i + '" class="readonly width" readonly="readonly">';
                       html+= '</fieldset>';
               	   }
               }
               html+= '<fieldset class="standart"><legend>' + all_align + ', ' + all_alt + '</legend>';
               html+= '<select name="images[' + id + '][image_align]">';
               html+= '<option value="right">' + all_right + '</option>';
               html+= '<option value="left">' + all_left + '</option>';
               html+= '</select>';
               html+= '&nbsp;&nbsp;<input type="text" name="images[' + id + '][image_alt]" size="25" maxlength="255" value="">';
               html+= '</fieldset>';
               html+= '</td><td class="work-lite">';
               html+= '<a class="topnolink" href="javascript:$.filebrowserimsremove(\'' + id + '\');">x</a>';
               html+= '</td></tr></table>';
               html+= '</div><div class="clear"></div>';
               $("form[name=total-form] #image-area").append(html);
               $("form[name=total-form] #image-area #imginput" + id).show('normal');
               $("#imgid").attr({value:id});
               var c = 1;
               $("#image-area .imgcount").each(function(){
                   $("#image-area").addClass("image-area");
                   this.value = '{img' + c + '}'; c++;
               });
               jQuery.filebrowserclose();
            }
  	    }
    }
}

jQuery.filebrowserimscreate = function(image_thumb,img){
    if (image_thumb.length > 0 && image_thumb.match(/\.(?:jpe?g|gif|png|bmp)/i)) {
  	        var id = $("#imgid").attr('value');
            if (id) {
               id++;
               var html = '<div id="imginput' + id + '" style="display:none;">';
               html+= '<table class="work" cellpadding="1" cellspacing="1"><tr><td class="work-lite center" valign="top">';
               html+= '<img src="../up' + image_thumb + '" width="70">';
               html+= '<p class="pad"><br /><input type="text" size="6" maxlength="7" value="" class="imgcount readonly" readonly="readonly">';
               html+= '</td><td class="work-clip" width="100%">';
               html+= '<fieldset class="standart"><legend>' + all_thumb + '</legend>';
               html+= '<input type="text" name="images[' + id + '][image_thumb]" size="15" maxlength="255" value="up' + image_thumb + '" class="readonly width" readonly="readonly">';
               html+= '</fieldset>';
               if (img.length > 0 && img.match(/\.(?:jpe?g|gif|png|bmp)/i)) {
               	       html+= '<fieldset class="standart"><legend>' + all_img + '</legend>';
                       html+= '<input type="text" name="images[' + id + '][image]" size="15" maxlength="255" value="up' + img + '" class="readonly width" readonly="readonly">';
                       html+= '</fieldset>';
               }
               html+= '<fieldset class="standart"><legend>' + all_align + ', ' + all_alt + '</legend>';
               html+= '<select name="images[' + id + '][image_align]">';
               html+= '<option value="right">' + all_right + '</option>';
               html+= '<option value="left">' + all_left + '</option>';
               html+= '</select>';
               html+= '&nbsp;&nbsp;<input type="text" name="images[' + id + '][image_alt]" size="25" maxlength="255" value="">';
               html+= '</fieldset>';
               html+= '</td><td class="work-lite">';
               html+= '<a class="topnolink" href="javascript:$.filebrowserimsremove(\'' + id + '\');">x</a>';
               html+= '</td></tr></table>';
               html+= '</div><div class="clear"></div>';
               $("form[name=total-form] #image-area").append(html);
               $("form[name=total-form] #image-area #imginput" + id).show('normal');
               $("#imgid").attr({value:id});
               var c = 1;
               $("#image-area .imgcount").each(function(){
                   $("#image-area").addClass("image-area");
                   this.value = '{img' + c + '}'; c++;
               });
               jQuery.filebrowserclose();
            }
    }
}

jQuery.filebrowserimsremove = function(id){
    $("form[name=total-form] #image-area #imginput" + id).remove();
    var c = 1;
    $("#image-area .imgcount").each(function(){ this.value = '{img' + c + '}'; c++; });
    if (c == 1) $("#image-area").removeClass("image-area");
}


jQuery.personalupload = function(path){
    $.colorbox({
    	width:'50%',
        href:'filebrowser.php?dn=personal&ops=' + path
    });
}